package main

// 互斥锁
import (
	"fmt"
	"sync"
)

// 通过同步锁解决这个问题
var ticket1 = 100
var mutex sync.Mutex
var wg sync.WaitGroup

func main() {
	wg.Add(4)
	go saleTickets2("窗口1")
	go saleTickets2("窗口2")
	go saleTickets2("窗口3")
	go saleTickets2("窗口4")
	wg.Wait()
}

func saleTickets2(name string) {
	defer wg.Done()
	for {
		mutex.Lock()
		if ticket1 > 0 {
			fmt.Println(name, "售出", ticket1)
			ticket1--
		} else {
			mutex.Unlock()
			fmt.Println(name, "售罄,没有票了...")
			break
		}
		mutex.Unlock()
	}
}
